﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Modeling.Diagrams;
using Microsoft.VisualStudio.Modeling;

namespace UofCASE.AgileProductLinerDSL {
    
    public static class ConfigColoringHandler {
        
        public static void UpdateDiagramSelection(Store store, IList<Feature> inConfigFeatures) {
            
            IList<Feature> allFeatures = store.ElementDirectory.FindElements<Feature>();
            IList<Feature> notInConfigFeatures = new List<Feature>();

            foreach (var item in allFeatures) {
                if (!inConfigFeatures.Contains(item)) notInConfigFeatures.Add(item);
            }


            foreach (var feature in notInConfigFeatures) {
                LinkedElementCollection<PresentationElement> fShapes = PresentationViewsSubject.GetPresentation(feature);
                FeatureShape fShape = fShapes[0] as FeatureShape;
                fShape.GrayOut();
                foreach (var item in fShape.Link) {
                    ConnectConnector connector = item as ConnectConnector;
                    if (connector != null) connector.GrayOut();
                }
                if (feature.TestPort != null) {
                    LinkedElementCollection<PresentationElement> testPortShapes = PresentationViewsSubject.GetPresentation(feature.TestPort);
                    TestPortShape tPShape = testPortShapes[0] as TestPortShape;
                    tPShape.GrayOut();
                    foreach (var item in feature.TestPort.FeaturePorts) {
                        LinkedElementCollection<PresentationElement> featurePortShapes = PresentationViewsSubject.GetPresentation(item);
                        FeaturePortShape fPShape = featurePortShapes[0] as FeaturePortShape;
                        TestConnector con = fPShape.ToRoleLinkShapes[0] as TestConnector;
                        con.GrayOut();
                        LinkedElementCollection<PresentationElement> testShapes = PresentationViewsSubject.GetPresentation(item.Test);
                        TestShape tShape = testShapes[0] as TestShape;
                        tShape.GrayOut();
                    }
                }
            }

            foreach (var feature in inConfigFeatures) {
                LinkedElementCollection<PresentationElement> fShapes = PresentationViewsSubject.GetPresentation(feature);
                FeatureShape fShape = fShapes[0] as FeatureShape;
                fShape.ResetColor();
                //foreach (var item in fShape.Link) {
                //    ConnectConnector connector = item as ConnectConnector;
                //    if (connector != null) connector.ResetColor(); 
                //}

                foreach (var item in fShape.ToRoleLinkShapes) {
                    ConnectConnector connector = item as ConnectConnector;
                    if (connector != null) connector.ResetColor();
                }
                foreach (var item in fShape.FromRoleLinkShapes) {
                    ConnectConnector connector = item as ConnectConnector;
                    if (connector == null) break;
                    FeatureModelElementReferencesSubFeatureModelElements con = connector.ModelElement as FeatureModelElementReferencesSubFeatureModelElements;
                    if (con == null) break;
                    if (con.TargetFeatureModelElement is Alternative) connector.ResetColor();
                }

                //foreach (var item in fShape.ToRoleLinkShapes) {
                //    ConnectConnector connector = item as ConnectConnector;
                //    connector.ResetColor();
                //}
                //foreach (var item in fShape.FromRoleLinkShapes) {
                    
                //    (item as ConnectConnector).ResetColor();
                //}
                if (feature.TestPort != null) {
                    LinkedElementCollection<PresentationElement> testPortShapes = PresentationViewsSubject.GetPresentation(feature.TestPort);
                    TestPortShape tPShape = testPortShapes[0] as TestPortShape;
                    tPShape.ResetColor();
                    foreach (var item in feature.TestPort.FeaturePorts) {
                        LinkedElementCollection<PresentationElement> featurePortShapes = PresentationViewsSubject.GetPresentation(item);
                        FeaturePortShape fPShape = featurePortShapes[0] as FeaturePortShape;
                        TestConnector con = fPShape.ToRoleLinkShapes[0] as TestConnector;
                        con.ResetColor();
                        LinkedElementCollection<PresentationElement> testShapes = PresentationViewsSubject.GetPresentation(item.Test);
                        TestShape tShape = testShapes[0] as TestShape;
                        tShape.ResetColor();
                    }
                }
            }
                       
        }

        public static void ResetColoring(Store store) {
            UpdateDiagramSelection(store, store.ElementDirectory.FindElements<Feature>());
        }
    }
}
